// copyright 1993 Michael B. Johnson; some portions copyright 1994, MIT
// see COPYRIGHT for reuse legalities
//

#import "RIBSphere.h"
#import <stdio.h>


@implementation RIBSphere

+ initialize { return [RIBSphere setVersion:1], self; }

- (BOOL)hasBoundingBox { return YES; }

- init
{
  [super init];

  radius = 1.0;
  zMin = -1.0;
  zMax =  1.0;
  
  return self;
}

- setRadius:(RtFloat)newRadius 
     zMin:(RtFloat)newZMin zMax:(RtFloat)newZMax 
     thetaMax:(RtFloat)newThetaMax
     n:(int)newN tokens:(RtToken *)newTokens parms:(RtPointer *)newParms archiveVector:(char **)newArchiveVector
     printfTypeVector:(int *)newPrintfTypeVector printfNVector:(int *)newPrintfNVector
{  
   radius = newRadius;
   zMin = newZMin;
   zMax = newZMax;
   if (zMin < (-1 * radius))
   {  zMin = -1 * radius;
   }
   if (zMax > radius)
   {  zMax = radius;
   }

   thetaMax = newThetaMax;
   [self setN:newN tokens:newTokens parms:newParms archiveVector:newArchiveVector printfTypeVector:newPrintfTypeVector printfNVector:newPrintfNVector];

   dirtyBoundingBox = TRUE;
  
   return self;
}

- setRadius:(RtFloat)newRadius { radius = newRadius; dirtyBoundingBox = TRUE; return self; }
- setZMin:(RtFloat)newZMin { zMin = newZMin; dirtyBoundingBox = TRUE; return self; }
- setZMax:(RtFloat)newZMax { zMax = newZMax; dirtyBoundingBox = TRUE; return self; }

- (RtFloat)radius { return radius; } 
- (RtFloat)zMin { return zMin; }
- (RtFloat)zMax { return zMax; }

- calculateBoundingBoxStartingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime 
{
  if (thetaMax >= 180.0)
  {   boundingBox[0] =  -1.0 * radius;
  }
  else  // less than 180
  {  if (thetaMax >= 90.0)
     {  boundingBox[0] =  -1.0 * radius * (RtFloat)sin((double)(toRadians((thetaMax - 90.0))));
     }
     else
     {  //boundingBox[0] =  radius * (RtFloat)sin((double)(toRadians(thetaMax)));
        boundingBox[0] = 0.0;  // this is not true if zMax or abs(zMin) is less than radius
     }
  }
  boundingBox[1] = radius;

  if (thetaMax >= 270.0)
  {  boundingBox[2] = -1.0 * radius;
     boundingBox[3] = radius;
  }
  else
  {  if (thetaMax >= 180.0)
     {  boundingBox[2] = radius * sin((double)(toRadians((thetaMax))));
        boundingBox[3] = radius;
     }
     else
     {  if (thetaMax >= 90.0)
        {  boundingBox[2] = 0.0;
           boundingBox[3] = radius;
        }
        else
        {  boundingBox[2] = 0.0;
           boundingBox[3] = radius * sin((double)(toRadians((thetaMax))));
        }
     }
  }

  if (zMin < 0.0)
  {  if ((-1.0 * radius) < zMin)  // not a full sphere in negative Z
     {  boundingBox[4] = zMin;
     }
     else
     {  boundingBox[4] = -1.0 * radius;
     }
  }
  else
  {  boundingBox[4] = zMin;
  }

  if (zMax < 0.0)
  {  boundingBox[5] = zMax;
  }
  else
  {  boundingBox[5] = radius;
  }

  //fprintf(stderr, "RIBSphere bbox:\n");
  //fprintf(stderr, "\t bbox : X (%f, %f)\n", boundingBox[0], boundingBox[1]);
  //fprintf(stderr, "\t      : Y (%f, %f)\n", boundingBox[2], boundingBox[3]);
  //fprintf(stderr, "\t      : Z (%f, %f)\n", boundingBox[4], boundingBox[5]);

  dirtyBoundingBox = FALSE; 
  return self;
}

- renderSelf:(WW3DCamera *)camera startingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
{
  RiSphereV(radius, zMin, zMax, thetaMax, n, tokens, parms);

  return self;
}

- (BOOL)theSameAs:otherRIBCommand
{
  if (radius != [otherRIBCommand radius])
  {  return NO;
  }
  if (zMin != [otherRIBCommand zMin])
  {  return NO;
  }
  if (zMax != [otherRIBCommand zMax])
  {  return NO;
  }

  return [super theSameAs:otherRIBCommand];
}



- (BOOL)isLerpable { return YES; }

// note: because we've made the WWSampleList "safe" for having
// multiple samples with the same data, it's perfectly valid to return
// yourself or b
- lerpWith:b by:(float)uValue
{
   id      newMe = nil;
   RtFloat radiusA, zMinA, zMaxA, thetaMaxA;
   RtFloat radiusB, zMinB, zMaxB, thetaMaxB;


   if (([self class] != [b class]) || (uValue <= 0.0))
   {  return self;
   }

   if (uValue >= 1.0)
   {  return b;
   }

   newMe = [super lerpWith:b by:uValue]; // this makes a copy for us

   // okay, now do the specific stuff for this class
   radiusA = [self radius];
   radiusB = [b radius];
   [newMe setRadius:(radiusA + ((radiusB - radiusA) * uValue))]; 
   zMinA = [self zMin];
   zMinB = [b zMin];
   [newMe setZMin:(zMinA + ((zMinB - zMinA) * uValue))];
   zMaxA = [self zMax];
   zMaxB = [b zMax];
   [newMe setZMax:(zMaxA + ((zMaxB - zMaxA) * uValue))];
   thetaMaxA = [self thetaMax];
   thetaMaxB = [b thetaMax];
   [newMe setThetaMax:(thetaMaxA + ((thetaMaxB - thetaMaxA) * uValue))];
 
   return newMe;
}


- lerpSelfWith:b by:(float)uValue
{
   RtFloat radiusA, zMinA, zMaxA, thetaMaxA;
   RtFloat radiusB, zMinB, zMaxB, thetaMaxB;


   if (([self class] != [b class]) || (uValue <= 0.0))
   {  return self;
   }

   if (uValue >= 1.0)
   {  return b;
   }

   [super lerpSelfWith:b by:uValue]; // this makes a copy for us

   // okay, now do the specific stuff for this class
   radiusA = [self radius];
   radiusB = [b radius];
   [self setRadius:(radiusA + ((radiusB - radiusA) * uValue))]; 
   zMinA = [self zMin];
   zMinB = [b zMin];
   [self setZMin:(zMinA + ((zMinB - zMinA) * uValue))];
   zMaxA = [self zMax];
   zMaxB = [b zMax];
   [self setZMax:(zMaxA + ((zMaxB - zMaxA) * uValue))];
   thetaMaxA = [self thetaMax];
   thetaMaxB = [b thetaMax];
   [self setThetaMax:(thetaMaxA + ((thetaMaxB - thetaMaxA) * uValue))];

   return self;
}



- writeEve:(NXStream *)stream atTabLevel:(int)tab
{
   int  i;


   for (i = 0; i < tab; i++)
   {  NXPrintf(stream, "\t");
   }
   NXPrintf(stream, "Sphere %f %f %f %f", radius, zMin, zMax, thetaMax);
   [super writeParameterList:stream];
   return self;
}


- writeInventorAtTime:(float)currentTime to:(NXStream *)stream atTabLevel:(int)tab
{
   RtFloat umax;
   RtFloat uknots[12];
   RtFloat uk[12] = { 0.0, 0.0, 0.0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1.0, 1.0, 1.0};
   RtFloat vmin, vmax;
   RtFloat vknots[8];
   RtFloat vk[8] = { 0.0, 0.0, 0.0, 0.5, 0.5, 1.0, 1.0, 1.0};
   RtFloat uspline[9][4] = { {1.0, 0.0, 1.0, 1.0},
			     {M_SQRT1_2, M_SQRT1_2, M_SQRT1_2, M_SQRT1_2},
			     {0.0, 1.0, 1.0, 1.0},
			     {-M_SQRT1_2, M_SQRT1_2, M_SQRT1_2, M_SQRT1_2},
			     {-1.0, 0.0, 1.0, 1.0},
			     {-M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2, M_SQRT1_2},
			     {0.0, -1.0, 1.0, 1.0},
			     {M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2, M_SQRT1_2},
			     {1.0, 0.0, 1.0, 1.0}};
   RtFloat vspline[5][4] = { {0.0, 0.0, -1.0, 1.0},
			     {M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2, M_SQRT1_2},
			     {1.0, 1.0, 0.0, 1.0},
			     {M_SQRT1_2, M_SQRT1_2, M_SQRT1_2, M_SQRT1_2},
			     {0.0, 0.0, 1.0, 1.0}};
   RtFloat r[4];
   int i, j, k;


   umax = [self thetaMax] / 360.0;
   for (i=0; i<12; i++)
	uknots[i] = uk[i] / umax;

   vmin = [self zMin] / [self radius];
   vmax = [self zMax] / [self radius];
   if (vmin < -1.0) vmin = -1.0;
   if (vmax > 1.0) vmax = 1.0;
   vmin = (asin(vmin) + M_PI_2) / M_PI;
   vmax = (asin(vmax) + M_PI_2) / M_PI;
   for (i=0; i<8; i++)
	vknots[i] = (vk[i] - vmin) / (vmax - vmin);

   r[0] = [self radius];
   r[1] = [self radius];
   r[2] = [self radius];
   r[3] = 1.0;

   for (i = 0; i < tab; i++)
   {  NXPrintf(stream, "\t");
   }
   NXPrintf(stream, "# ");
   [self writeEve:stream atTabLevel:0];
   NXPrintf(stream, "\n");

   for (i = 0; i < tab; i++)
   {  NXPrintf(stream, "\t");
   }
   NXPrintf(stream, "# since Inventor doesn't directly support quadrics in any terribly useful way (sigh), we turn it into a NURBS surface:\n");

   for (i = 0; i < tab; i++)
   {  NXPrintf(stream, "\t");
   }
   NXPrintf(stream, "Separator {\n");

   for (i = 0; i < (tab+1); i++)  {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "Coordinate4 {\n");
   for (i = 0; i < (tab+2); i++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "point [");

   for (i=0; i<4; i++)
   {  for (j=0; j<9; j++)
      {  if (!i && !j)
         {  NXPrintf(stream, 
		     "%f %f %f %f,\n", 
		     (r[0] * uspline[j][0] * vspline[i][0]),
		     (r[1] * uspline[j][1] * vspline[i][1]),
		     (r[2] * uspline[j][2] * vspline[i][2]),
		     (r[3] * uspline[j][3] * vspline[i][3]));
 	 }
         else
         {  NXPrintf(stream, 
		     "       %f %f %f %f,\n", 
		     (r[0] * uspline[j][0] * vspline[i][0]),
		     (r[1] * uspline[j][1] * vspline[i][1]),
		     (r[2] * uspline[j][2] * vspline[i][2]),
		     (r[3] * uspline[j][3] * vspline[i][3]));
 	 }
         for (k = 0; k < (tab+2); k++) {  NXPrintf(stream, "\t"); }
      }
   }
   for (j=0; j<8; j++)
   {  NXPrintf(stream, 
               "       %f %f %f %f,\n", 
               (r[0] * uspline[j][0] * vspline[i][0]),
               (r[1] * uspline[j][1] * vspline[i][1]),
               (r[2] * uspline[j][2] * vspline[i][2]),
               (r[3] * uspline[j][3] * vspline[i][3]));
     for (k = 0; k < (tab+2); k++) {  NXPrintf(stream, "\t"); }
   }
   NXPrintf(stream, 
	    "       %f %f %f %f]\n", 
	    (r[0] * uspline[j][0] * vspline[i][0]),
	    (r[1] * uspline[j][1] * vspline[i][1]),
	    (r[2] * uspline[j][2] * vspline[i][2]),
	    (r[3] * uspline[j][3] * vspline[i][3]));
   for (k = 0; k < (tab+1); k++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "}\n");

   for (k = 0; k < (tab+1); k++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "NurbsSurface {\n");
   for (k = 0; k < (tab+2); k++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "numUControlPoints 9\n");
   for (k = 0; k < (tab+2); k++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "numVControlPoints 5\n");

   for (k = 0; k < (tab+2); k++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "uKnotVector [");
   for (i=0; i<11; i++)
   {	NXPrintf(stream, "%f, ", uknots[i]);
   }
   NXPrintf(stream, "%f]\n", uknots[i]);

   for (k = 0; k < (tab+2); k++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "vKnotVector [");
   for (i=0; i<7; i++)
   {	NXPrintf(stream, "%f, ", vknots[i]);
   }
   NXPrintf(stream, "%f]\n", vknots[i]);
   for (k = 0; k < (tab+1); k++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "}\n");
   for (k = 0; k < tab; k++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "}\n");

   return self;
}

#define typeVector "fff"
#define typeValues &radius, &zMin, &zMax

- read:(NXTypedStream*)stream 
{
    int version;
    [super read:stream];

    version = NXTypedStreamClassVersion(stream,"RIBSphere");
    if (version == 0) NXReadTypes(stream,"i",&version), version=1;
    if (version == 1)
    {  NXReadTypes(stream,typeVector,typeValues);
    } 
    else 
    {
    }
    return self;
}

- write:(NXTypedStream*)stream 
{
    [super write:stream];

    NXWriteTypes(stream,typeVector, typeValues);

    return self;
}


@end
